from datetime import datetime, date, timedelta
import time
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse


# 判断是否闰年
def is_leap(year):
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0


# 获取某年某月的天数
def get_days(year, month):
    if month in [1, 3, 5, 7, 8, 10, 12]:
        return 31
    elif month == 2:
        return 29 if is_leap(year) else 28
    else:
        return 30

# 获取当季已走完的月份数
def get_months_qua_now():
    ticks = time.localtime(time.time())
    now_month = ticks[1]
    now_qua = int((int(now_month) - 1) / 3) + 1
    return now_month-(now_qua-1)*3

def get_last_month(number):
    # 获取前几个月
    month_date = datetime.now().date() - relativedelta(months=number)
    return month_date.strftime("%Y-%m")

def get_last_month_day(number):
    # 当前时间
    month_date = datetime.now().date() - relativedelta(months=number)
    print(month_date.year)
    year = month_date.year
    month = month_date.month
    months = month
    if month<10:
        months ="0"+str(month)
    day = get_days(month_date.year,month_date.month)
    return str(year)+"-"+str(months)+"-"+str(day)

def get_old_month_qua_las_day():
    month_ed = get_months_qua_now()
    month_id = datetime.now().strftime("%Y-%m")
    day_list = []
    if month_ed > 1:
        for i in range(month_ed):
            last_month = get_last_month(i)
            last_month_day = get_last_month_day(i)
            if month_id != get_last_month(i):
                day_list.append(get_last_month_day(i))
    return day_list


# 返回的是时间类型
def get_day_list(start_day_str=None, end_day_str=None):

    start_day = datetime.strptime(start_day_str, '%Y-%m-%d')
    end_day = datetime.strptime(end_day_str, '%Y-%m-%d')
    day_list = []
    curr_day = start_day
    while curr_day <= end_day:
        day_list.append(curr_day)
        curr_day = curr_day + relativedelta(days=1)

    return day_list


if __name__ == '__main__':
    import re
    text = '{"score": "650.09", "score": "-9999", "name": "qqqq"}'
    pattern = r'"score": "-?\d+(\.\d+)?'
    matches = re.findall(pattern, text)
    print(matches)



    ts_nodash = '20231022T071000'
    print(ts_nodash[0:8] + ' ' + ts_nodash[9:11])
    new_ts_nodash = ts_nodash.replace('T', ' ')
    print(new_ts_nodash)
    load_day = datetime.strptime(new_ts_nodash, '%Y%m%d %H%M%S')
    load_day = load_day + relativedelta(hours=-1)
    print('load_day: ' + str(load_day))
    print('load_day: ' + str(load_day)[0:10] + '-' + str(load_day)[11:13])

    from datetime import datetime, timedelta

    input_datetime_str = "2023-08-22 10:00:00"
    input_datetime = datetime.strptime(input_datetime_str, "%Y-%m-%d %H:%M:%S")
    # 减去一个小时
    previous_hour_datetime = input_datetime - timedelta(hours=1)
    print("输入的日期时间:", input_datetime)
    print("上一个小时的日期时间:", previous_hour_datetime)
